<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/4/1
 * Time: 15:55
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\SeminarTemplatePageTitleModel;

/**
 * Class SeminarTitleService
 * @package SDXApi\Service
 */
class SeminarTitleService extends TemplateAbstract
{
	/**
	 * @var SeminarTemplatePageTitleModel
	 */
	protected $seminarPageTitleModel;

	/**
	 * SeminarTitleService constructor.
	 */
	public function __construct()
	{
		$this->seminarPageTitleModel = new SeminarTemplatePageTitleModel();
	}

	/**
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return array|mixed
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
		$cacheName = 'seminar_template_page_title_' . $template['id'];
		$data = $this->getCache($cacheName);

		if (empty($data)) {
			$data = $this->title($template);

			if (! empty($data)) {
				$this->cacheData($cacheName, $data, mt_rand(3600, 4200));
			}
		}

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param array $page
	 * @return array
	 */
	protected function title($page = array())
	{
		$title = $this->seminarPageTitleModel->findSeminarPageTitleByPageId($page['id']);

		if (empty($title)) {
			$title = array();
		}

		$page['title'] = $title;
		unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['image_url'], $page['image_link'], $page['image_size'], $page['template_id'], $page['goods_type'], $page['goods_id']);
		return $page;
	}
}